package com.dzienniczekwedkarza.activities;


import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.dzienniczekwedkarza.R;

/**
 * @author Rafał Wysogląd
 * 
 */

public class MainMenuActivity extends Activity {

	private Button buttonDziennikPolowow;
	private Button buttonAtlasRyb;
	private Button buttonRegulaminyIPrzepisy;
    private Button buttonOAplikacji;
    private Button buttonWyloguj;
    
    private String[] drawerListViewItems;
    private ListView drawerListView;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    
  
  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		//http://stackoverflow.com/questions/10352628/cannot-resolve-style-theme-sherlock
		if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
		    this.setTheme(com.actionbarsherlock.R.style.Theme_Sherlock);
		}
		
		super.onCreate(savedInstanceState);
		
		// to create a custom title bar for activity window
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
		
		setContentView(R.layout.activity_main_menu);
		
		
		 // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.drawer_navigation_list);
        
        // get ListView defined in activity_main_menu.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);
 
        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_listview_item, drawerListViewItems));
        
       // App Icon
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        
        
        //enable ActionBar app icon to behave as action to toggle nav drawer
        // Enabling Home button
        // getActionBar().setHomeButtonEnabled(true);
        // Enabling Up navigation
        // getActionBar().setDisplayHomeAsUpEnabled(true);
        
        actionBarDrawerToggle = new ActionBarDrawerToggle(
        		this,                       /* host activity*/
        		drawerLayout,               /* drawerLayout object*/
        		R.drawable.ic_drawer,     /* nav drawer icon to replace 'Up' caret */
        		R.string.drawer_open,       /* "open drawer" description */
        		R.string.drawer_close       /* "close drawer" description */
        );
         
        // 2.2 Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
 
        // 2.3 enable and show "up" arrow
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
     
        // just styling option
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
 
        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
        
   
				
		// use custom layout for title bar. make sure to use it after setContentView()
		//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
		
	    //TextView txt = (TextView) findViewById(R.id.title_text_menu);  
	   // Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Existence-Light.otf");  
	    //txt.setTypeface(font);  
		
					
		inicjujElementyUI();
		reakcjaNaButtonyOnClickListeners();
	}
	
	// koniec onCreate
	
	
	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
	        super.onConfigurationChanged(newConfig);
	        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }
	 
	@Override    
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
            
    // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
    // then it has handled the app icon touch event

    if (actionBarDrawerToggle.onOptionsItemSelected(null) )
    {
        return true;
    }
        return super.onOptionsItemSelected(item);
    }
    
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
    	
    
     public void onItemClick(AdapterView <?> parent, View view, int position, long id) {
             Toast.makeText(MainMenuActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
             drawerLayout.closeDrawer(drawerListView);

     }
    }

    
    private void inicjujElementyUI() {
    	buttonDziennikPolowow = (Button) findViewById(R.id.buttonDziennikPolowow);
    	buttonAtlasRyb = (Button) findViewById(R.id.buttonAtlasRyb);
    	buttonRegulaminyIPrzepisy = (Button) findViewById(R.id.buttonRegulaminyIPrzepisy);
    	buttonOAplikacji = (Button) findViewById(R.id.buttonOAplikacji);
    	buttonWyloguj = (Button) findViewById(R.id.buttonWyloguj);
    	
	}
	
    private void reakcjaNaButtonyOnClickListeners() {
        OnClickListener onClickListener = new OnClickListener() {
            public void onClick(View v) {
                switch (v.getId()) {
                  	case R.id.buttonDziennikPolowow: DodajWypraweActivity(); 
                  	break;
                  	case R.id.buttonAtlasRyb: ListaStatycznaAtlasRybActivity();
                  	break;
                  	case R.id.buttonRegulaminyIPrzepisy: ListaRegulaminyIPrzepisyActivity();
                  	break;
                  	case R.id.buttonOAplikacji: OAplikacjiActivity();
                  	break;
                  	case R.id.buttonWyloguj: ZamknijAplikacje();
                  	break;
                  	
                  	
                default: break;
                }
            }

			
        };
        buttonDziennikPolowow.setOnClickListener(onClickListener);
        buttonAtlasRyb.setOnClickListener(onClickListener);
        buttonRegulaminyIPrzepisy.setOnClickListener(onClickListener);
        buttonOAplikacji.setOnClickListener(onClickListener);
        buttonWyloguj.setOnClickListener(onClickListener);
        
    }
	
    
    
  
    
    
       
    private void DodajWypraweActivity() {
		Intent intent = new Intent(getApplicationContext(), DodajWypraweActivity.class);
		startActivity(intent);
	}
    
    
    private void ListaRegulaminyIPrzepisyActivity() { 
    	Intent intent = new Intent(getApplicationContext(), ListViewStaticRegulaminyIPrzepisyActivity.class);
    	startActivity(intent);
    }
    
	
	private void ListaStatycznaAtlasRybActivity() {
		Intent intent = new Intent(getApplicationContext(), ListViewStaticAtlasRybActivity.class);
		startActivity(intent);
	}
	
	private void  OAplikacjiActivity() {
		Intent intent = new Intent(getApplicationContext(), AboutTheApplicationActivity.class);
		startActivity(intent);
	}
	
	
	private void ZamknijAplikacje() {
           finish();          
           System.exit(0);
           
           
    }
	
	


	
	
}
